package com.ruoyi.web.controller.clothing;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.clothing.domain.User;
import com.ruoyi.clothing.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.clothing.domain.SysCart;
import com.ruoyi.clothing.service.ISysCartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购物车管理Controller
 *
 * @author weh
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/clothing/cart")
public class SysCartController extends BaseController
{
    @Autowired
    private ISysCartService sysCartService;

    @Autowired
    private IUserService userService;

    /**
     * 查询购物车管理列表
     */
//    @PreAuthorize("@ss.hasPermi('clothing:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCart sysCart)
    {
        startPage();
        List<SysCart> list = sysCartService.selectSysCartList(sysCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车管理列表
     */
//    @PreAuthorize("@ss.hasPermi('clothing:cart:export')")
    @Log(title = "购物车管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCart sysCart)
    {
        List<SysCart> list = sysCartService.selectSysCartList(sysCart);
        ExcelUtil<SysCart> util = new ExcelUtil<SysCart>(SysCart.class);
        util.exportExcel(response, list, "购物车管理数据");
    }

    /**
     * 获取购物车管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('clothing:cart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sysCartService.selectSysCartById(id));
    }

    /**
     * 新增购物车管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:cart:add')")
    @Log(title = "购物车管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCart sysCart)
    {
        return toAjax(sysCartService.insertSysCart(sysCart));
    }

    /**
     * 修改购物车管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:cart:edit')")
    @Log(title = "购物车管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCart sysCart)
    {
        return toAjax(sysCartService.updateSysCart(sysCart));
    }

    /**
     * 删除购物车管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:cart:remove')")
    @Log(title = "购物车管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysCartService.deleteSysCartByIds(ids));
    }

    /**
     * 根据用户名查询购物车信息
     * @param username 用户名
     * @return 购物车列表
     */
    @GetMapping("/selectCartListByUsername")
    public List<SysCart> selectCartListByUsername(@RequestParam String username){
        User user = userService.selectUserByUserName(username);
        Long id = user.getId();
        return sysCartService.selectCartListById(id);
    }

    /**
     * 根据id删除购物车数据
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCartById")
    public AjaxResult deleteCartById(@RequestParam Integer id){
        sysCartService.deleteCartById(id);
        return AjaxResult.success("删除购物车成功");
    }
}
